package com.hc.mall.auth.controller;

import com.alibaba.fastjson.TypeReference;
import com.hc.common.utils.R;
import com.hc.mall.auth.feign.MemberFeignService;
import com.hc.mall.auth.vo.UserLoginVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.util.HashMap;
import java.util.Map;

@Controller
public class LoginController {
    @Autowired
    MemberFeignService memberFeignService;
    @PostMapping("/login")
    public String login(UserLoginVo vo, RedirectAttributes attributes){
        R login = memberFeignService.login(vo);
        if(login.getCode() == 0){
            return "redirect:http://mall.com";
        }else{
            Map<String,String> map = new HashMap<>();
            map.put("msg",login.getData("msg",new TypeReference<String>(){}));
            attributes.addFlashAttribute("errors",map);
            return "redirect:http://auth.mall.com/login.html";
        }
    }
}
